package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AppFunctionConfiguration;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BaseInvitationCode;
import com.ruoyi.system.service.IBaseInvitationCodeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 邀请码配置Controller
 *
 * @author ruoyi
 * @date 2023-03-08
 */
@RestController
@RequestMapping("/system/code")
public class BaseInvitationCodeController extends BaseController {
    @Autowired
    private IBaseInvitationCodeService baseInvitationCodeService;

    /**
     * 查询邀请码配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:code:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseInvitationCode baseInvitationCode) {
        startPage();
        List<BaseInvitationCode> list = baseInvitationCodeService.selectBaseInvitationCodeList(baseInvitationCode);
        return getDataTable(list);
    }

    /**
     * 导出邀请码配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:code:export')")
    @Log(title = "邀请码配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseInvitationCode baseInvitationCode) {
        List<BaseInvitationCode> list = baseInvitationCodeService.selectBaseInvitationCodeList(baseInvitationCode);
        ExcelUtil<BaseInvitationCode> util = new ExcelUtil<BaseInvitationCode>(BaseInvitationCode.class);
        util.exportExcel(response, list, "邀请码配置数据");
    }

    /**
     * 获取邀请码配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:code:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(baseInvitationCodeService.selectBaseInvitationCodeById(id));
    }

    /**
     * 新增邀请码配置
     */
    @PreAuthorize("@ss.hasPermi('system:code:add')")
    @Log(title = "邀请码配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseInvitationCode baseInvitationCode) {
        // 判断邀请码 是否已存在
        Long count = baseInvitationCodeService.count(new QueryWrapper<BaseInvitationCode>().eq("invitation_code", baseInvitationCode.getInvitationCode()));
        if (count > 0) {
            return error("请更换邀请码，此邀请码已存在！");
        }
        if (StringUtils.isEmpty(baseInvitationCode.getDefaultUser())) {
            return error("默认好友必填！");
        }
        String[] userIds = baseInvitationCode.getDefaultUser().split(",");
        if (userIds.length > 0) {
            baseInvitationCode.setNextOne(userIds[0]);
        }

        return toAjax(baseInvitationCodeService.insertBaseInvitationCode(baseInvitationCode));
    }

    /**
     * 修改邀请码配置
     */
    @PreAuthorize("@ss.hasPermi('system:code:edit')")
    @Log(title = "邀请码配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseInvitationCode baseInvitationCode) {
        Long count = baseInvitationCodeService.count(
                new QueryWrapper<BaseInvitationCode>()
                        .ne("id", baseInvitationCode.getId())
                        .eq("invitation_code", baseInvitationCode.getInvitationCode()));
        if (count > 0) {
            return error("请更换邀请码，此邀请码已存在！");
        }
        if (StringUtils.isEmpty(baseInvitationCode.getDefaultUser())) {
            return error("默认好友必填！");
        }
        String[] userIds = baseInvitationCode.getDefaultUser().split(",");
        if (userIds.length > 0) {
            baseInvitationCode.setNextOne(userIds[0]);
        }
        return toAjax(baseInvitationCodeService.updateBaseInvitationCode(baseInvitationCode));
    }

    /**
     * 删除邀请码配置
     */
    @PreAuthorize("@ss.hasPermi('system:code:remove')")
    @Log(title = "邀请码配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(baseInvitationCodeService.deleteBaseInvitationCodeByIds(ids));
    }
}
